package controllers

import (
	"boxapi/dao"
	"boxapi/models"
	"fmt"

	"github.com/astaxie/beego"
	"gopkg.in/mgo.v2"
)

type JobController struct {
	beego.Controller
}

func (j *JobController) Get() {
	defer j.ServeJSON()
	job := j.Ctx.Input.Param(":job")
	if job != "java" {
		j.Data["json"] = []int{}
		return
	}
	var jobs []models.Job
	//>>> 查询 (可以分装在service层中)
	query := func(c *mgo.Collection) error {
		return c.Find(nil).All(&jobs)
	}
	err := dao.Execute("javajobs", query)
	// 查询 (可以分装在service层中) <<<
	if err != nil {
		// 失败
		var errMsg models.ErrMsg
		errMsg.Msg = "db err"
		j.Data["json"] = errMsg
	} else {
		// 成功
		// fmt.Println(jobs)
		j.Data["json"] = jobs
		fmt.Println(jobs)
		fmt.Println(len(jobs))
	}
}
